<?php

/*
    Запрос: Кто играл более 14 раз, но при этом ни одной игры не засчиталось.
    select sum(amount) summa, count(*) cnt, user
    from PLAYLOG
    group by user
    having summa is null and cnt > 14
    order by cnt DESC, summa DESC
    LIMIT 0, 999
*/
    global $mikron;

    if(isset($_GET['ac']))
    {
        // Открыли аккаунт на редактирование
        $cabid = (int)$_GET['ac'];
        echo "<h1>Редактирование аккаунта:</h1>&nbsp;
        {{userform entityname=T_USER id=$cabid caption=&nbsp; captcha=0 returnpage=\"/?ac=$cabid\" processor=/}}";
        return;
    }
    ?><h1>Проверка системы:</h1><?php

    ?>&nbsp;<h1>Ошибка(и) проверки заявок на вывод:</h1><?php
    $tbl = null;
    $exports = $mikron->Queries->Query('T_EX_RECORD', null, "`status` = 1");
    while($export = $exports->fetch())
    {
        $iserror = false;
        $errortext = null;
        $oplog = null;
        if(is_null($export->operationlog->value))
        {
            $errortext = 'Отсутствует лог операции';
            $iserror = true;
        }
        if(!$iserror)
        {
            $oplog = $export->operationlog->value;
            $log_amount = -$oplog->amount->value;
            $exp_amount = $export->amount->value;
            if($log_amount != $exp_amount)
            {
                $errortext = 'Несовпадение суммы с записью из логов';
                $iserror = true;
            }
        }
        if(!$iserror)
        {
            $ecp = $oplog->ecp->value;
            $oplog->ecp->value = null;
            $cab = $export->cabinet->value;
            $dt = $oplog->dt->value;
            $oplog->dt->value = date($mikron->Constants->DateTimeFormat, strtotime($oplog->dt->value));
            OperationLogEcp($oplog, $cab);
            if($oplog->ecp->value != $ecp)
            {
                $errortext = 'Нарушена целостность';
                $iserror = true;
            }
        }
        if($iserror)
        {
            if(is_null($tbl))
            {
                $tbl = new html_table(null, "table4");
                $tbl->addhead("Аккаунт");
                $tbl->addhead("Заявка");
                $tbl->addhead("Сообщение");
            }
            $cabid = $export->cabinet->value->id;
            $cblogin = $export->cabinet->value->login->value;
            $tbl->newcol("<a href=\"/?ac=$cabid\">$cblogin</a>");
            $tbl->newcol($export->id);
            $tbl->newcol($errortext);
        }
    }
    if(is_null($tbl))
    {
        ?>Проверка ошибок не вывела.<br /><?php
    }
    else
    {
        $tbl->end();
    }
    echo 'Проверено заявок &ndash; ', $exports->count(), '<br />&nbsp;<br />';

    ?><h1>Проверка целостности аккаунтов:</h1><?php
    $cabinets = $mikron->Queries->Query('T_USER', null, new Criterion('amount', 0, CR_MORE), null, null, null, null, Array('amount', 'bronze', 'silver', 'gold', 'platinum'));

    $tbl = null;
    while($checkedcabinet = $cabinets->fetch())
    {
        $amount = 0;
        $bronze = 0;
        $silver = 0;
        $gold = 0;
        $platinum = 0;
        $cabid = $checkedcabinet->id;
        $logs = $mikron->Queries->Query('T_OPERATIONS_LOG', null, new Criterion('cabinet', $cabid), null, null, null, false, Array('amount', 'bronze', 'silver', 'gold', 'platinum'));
        while($log = $logs->fetch())
        {
            $amount += $log->amount->value;
            $bronze += $log->bronze->value;
            $silver += $log->silver->value;
            $gold += $log->gold->value;
            $platinum += $log->platinum->value;
        }
        unset($logs);
        $cabamount = $checkedcabinet->amount->value;
        $cabbronze = $checkedcabinet->bronze->value;
        $cabsilver = $checkedcabinet->silver->value;
        $cabgold = $checkedcabinet->gold->value;
        $cabplatinum = $checkedcabinet->platinum->value;
        
        if(!(($cabamount == $amount) && ($cabbronze == $bronze) && ($cabsilver == $silver) && ($cabgold == $gold) && ($cabplatinum == $platinum)))
        {
            $cabamount /= 100;
            $amount /= 100;
            if(is_null($tbl))
            {
                $tbl = new html_table(null, "table4");
                $tbl->addhead("Аккаунт");
                $tbl->addhead("Баланс");
                $tbl->addhead("Бронза");
                $tbl->addhead("Серебро");
                $tbl->addhead("Золото");
                $tbl->addhead("Платина");
            }
            $cblogin = $checkedcabinet->login->value;
            $tbl->newrow();
            $tbl->newcol("<a href=\"/?ac=$cabid\">$cblogin</a>");
            $tbl->newcol();
                if($cabamount!=$amount){echo $cabamount, "<font color=\"#aaa\"> руб из</font> $amount";}else{?>&nbsp;<?php }
            $tbl->newcol();
                if($cabbronze!=$bronze){echo "$cabbronze/$bronze";}else{?>&nbsp;<?php }
            $tbl->newcol();
                if($cabsilver!=$silver){echo "$cabsilver/$silver";}else{?>&nbsp;<?php }
            $tbl->newcol();
                if($cabgold!=$gold){echo "$cabgold/$gold";}else{?>&nbsp;<?php }
            $tbl->newcol();
                if($cabplatinum!=$platinum){echo "$cabplatinum/$platinum";}else{?>&nbsp;<?php }
        }
    }

    if(is_null($tbl))
    {
        ?>Проверка ошибок не вывела.<br /><?php
    }
    else
    {
        $tbl->end();
    }
    echo "Проверено аккаунтов &ndash; ", $cabinets->count();

?>